﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WinFormUI
{
    public partial class Form1 : Form
    {
        NFLDriverLogic dr = new NFLDriverLogic();
        bool enableTeamSelection = true;
        public Form1()
        {
            InitializeComponent();
            populateListBoxes();
            //populateListbox(1);
            //populateListbox(2);
            /*
            listBox1.Items.Add("New England Patriots");
            listBox1.Items.Add("Miami Dolphins");
            listBox1.Items.Add("Cincinnati Bengals");
            listBox1.Items.Add("Baltimore Ravens");
            listBox1.Items.Add("Indianapolis Colts");
            listBox1.Items.Add("Denver Broncos");
            listBox1.Items.Add("Kansas City Chiefs");
            listBox1.Items.Add("Philadelphia Eagles");
            listBox1.Items.Add("New Orleans Saints");
            listBox1.Items.Add("Carolina Panthers");
            listBox1.Items.Add("Seattle Seahawks");
            listBox1.Items.Add("San Francisco 49ers");
            listBox2.Items.Add("New York Jets");
            listBox2.Items.Add("San Diego Chargers");
            listBox2.Items.Add("Dallas Cowboys");
            listBox2.Items.Add("Detroit Lions");
            listBox2.Items.Add("Chicago Bears");
            listBox2.Items.Add("Green Bay Packers");
            listBox2.Items.Add("Arizona Cardinals");
            listBox3.Items.Add("Buffalo Bills");
            listBox3.Items.Add("Pittsburgh Steelers");
            listBox3.Items.Add("Cleveland Browns");
            listBox3.Items.Add("Tennessee Titans");
            listBox3.Items.Add("Jacksonville Jaguars");
            listBox3.Items.Add("Houston Texans");
            listBox3.Items.Add("Oakland Raiders");
            listBox3.Items.Add("New York Giants");
            listBox3.Items.Add("Washington Redskins");
            listBox3.Items.Add("Minnesota Vikings");
            listBox3.Items.Add("Tampa Bay Buccaneers");
            listBox3.Items.Add("Atlanta Falcons");
            listBox3.Items.Add("St. Louis Rams");
            */
        }        //populates listbox corresponding to listBoxNumber (0-2)

        private void cmdExecute()
        {
            //SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=CS430_SeniorProjectDB;Integrated Security=True");

           // try
            //{
                //listBox1.Items.Clear();
                //lvResult.Columns.Clear();
                //lvResult.Items.Clear();

                //conn.Open();
                //txt.Text = "SELECT * FROM Teams";

                //SqlCommand cmd = conn.CreateCommand();
                //cmd.CommandText = txt.Text;

                //SqlDataReader dr = cmd.ExecuteReader();

                //for (int i = 0; i < dr.FieldCount; i++)
                //{
                //    ColumnHeader ch = new ColumnHeader();
                //    ch.Text = dr.GetName(i);
                //    lvResult.Columns.Add(ch);
                //}

                //ListViewItem itmX;

            //    while (dr.Read())
            //    {
            //        itmX = new ListViewItem();
            //        itmX.Text = dr.GetValue(0).ToString();

            //        for (int i = 1; i < dr.FieldCount; i++)
            //        {
            //            itmX.SubItems.Add(dr.GetValue(i).ToString());
            //        }
            //        lvResult.Items.Add(itmX);
            //    }
            //    dr.Close();
            //}
            //catch (System.Data.SqlClient.SqlException ex)
           // {
                //Console.WriteLine("Your db is fucked up breh!" +
                //"\nError Message:" + ex.Message, "SQL");
           // }
           // finally
           // {
              //  conn.Close();
            //}
       }

        /*
        private void populateListbox(int listBoxNumber)
        {
            switch (listBoxNumber)
            {
                case 0:
                    {
                        for (int i = 0; i < 12; i++)
                        {
                            if (dr.getTeams(i) != null)
                            {
                                listBox1.Items.Add(dr.getTeam(listBoxNumber, i).teamName);
                            }
                            else
                            {
                                MessageBox.Show("NFLDriver.getTeam(int columnNumber, int columnIndex) returned null at Form1.populateListbox()\ncolumnNumber = " + listBoxNumber, "Exception: Null Value", MessageBoxButtons.OK);
                            }
                        }
                        break;
                    }
                case 1:
                    {
                        for (int i = 0; i < 7; i++)
                        {
                            if (dr.getTeam(listBoxNumber, i) != null)
                            {
                                listBox2.Items.Add(dr.getTeam(listBoxNumber, i).teamName);
                            }
                            else
                            {
                                MessageBox.Show("NFLDriver.getTeam(int columnNumber, int columnIndex) returned null at Form1.populateListbox()\ncolumnNumber = " + listBoxNumber, "Exception: Null Value", MessageBoxButtons.OK);
                            }
                        }
                        break;
                    }
                case 2:
                    {
                        for (int i = 0; i < 13; i++)
                        {
                            if (dr.getTeam(listBoxNumber, i) != null)
                            {
                                listBox3.Items.Add(dr.getTeam(listBoxNumber, i).teamName);
                            }
                            else
                            {
                                MessageBox.Show("NFLDriver.getTeam(int columnNumber, int columnIndex) returned null at Form1.populateListbox()\ncolumnNumber = " + listBoxNumber, "Exception: Null Value", MessageBoxButtons.OK);
                            }
                        }
                        break;
                    }
                default:
                    {
                        MessageBox.Show("Invalid columnNumber value at Form1.populateListbox()\ncolumnNumber = " + listBoxNumber, "Exception: Invalid input", MessageBoxButtons.OK);
                        break;
                    }

            }
        }
        */
        private void populateListBoxes()
        {
            for (int i = 0; i < 32; i++)
            {
                if (dr.getTeams(i) != null)
                {
                    if(i<12)
                    {
                        dr.getTeams(i).teamColumn = 0;
                        listBox1.Items.Add(dr.getTeams(i).teamName);
                    }
                    else if(i<19)
                    {
                        dr.getTeams(i).teamColumn = 1;
                        listBox2.Items.Add(dr.getTeams(i).teamName);
                    }
                    else
                    {
                        dr.getTeams(i).teamColumn = 2;
                        listBox3.Items.Add(dr.getTeams(i).teamName);
                    }

                    //populateListBox(dr.getTeams(i).teamColumn, dr.getTeams(i).teamName);
                }
                else
                {
                    MessageBox.Show("Null reference at Form1.populateListboxs()\ndr.getTeams(" + i + ") = null" , "Exception: Null Reference", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void populateListBox(int listBoxNumber, string TeamName)
        {
            for(int i=0; i<32; i++)
            {

            }
            /*
            switch (listBoxNumber)
            {
                case 0:
                    {
                        listBox1.Items.Add(TeamName);
                        break;
                    }
                case 1:
                    {
                        listBox2.Items.Add(TeamName);
                        break;
                    }
                case 2:
                    {
                        listBox3.Items.Add(TeamName);
                        break;
                    }
                default:
                    {
                        MessageBox.Show("Invalid columnNumber value at Form1.populateListbox()\ncolumnNumber = " + listBoxNumber, "Exception: Invalid input", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                    }
             
            }
             */ 
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
           // if (listBox1.SelectedIndex)
            {

            }
        }

        private void listBox2_DoubleClick(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void listBox1_MouseClick(object sender, MouseEventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnAccount_Click(object sender, EventArgs e)
        {
            //dr.createAccountShow(dr);
            frmCreateAccount frmCreateAccountDialog = new frmCreateAccount();
            frmCreateAccountDialog.setDrInstance(dr);
            frmCreateAccountDialog.Show();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (enableTeamSelection)
            {
                
                dr.setUserTeam(listBox1.SelectedIndex, listBox1.SelectedItem.ToString());
                updateDataGridView();
            }
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (enableTeamSelection)
            {
                dr.setUserTeam(listBox2.SelectedIndex, listBox2.SelectedItem.ToString());
                //dr.getTeam(listBox3.SelectedItem.ToString());
                updateDataGridView();
            }
        }

        private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (enableTeamSelection)
            {
                dr.setUserTeam(listBox3.SelectedIndex, listBox3.SelectedItem.ToString());
                //dr.getTeam(listBox3.SelectedItem.ToString());
                updateDataGridView();
            }
        }

        private void updateDataGridView()
        {
            string columnId = "";
            if ((dataGridView1.RowCount > 0) && (dataGridView1.RowCount < 6))
            {
                /*
                for (int j = 0; j < dataGridView1.RowCount; j++)
                {
                    dataGridView1.Rows.RemoveAt(j);
                }
              */
                
            }
            for (int i = 0; i < 7; i++)
            {
                if (dr.getUserSelectedTeam(i) != null)
                {
                    if (dr.getUserSelectedTeam(i).teamColumn == 0)
                    {
                        columnId = "A";
                    }
                    else
                    {
                        if (dr.getUserSelectedTeam(i).teamColumn == 1)
                        {
                            columnId = "B";
                        }
                        else
                        {
                            if (dr.getUserSelectedTeam(i).teamColumn == 2)
                            {
                                columnId = "C";
                            }
                            else
                            {
                                MessageBox.Show("Invalid value at Form1.UpdateDataGridView()", "Invalid value", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    
                    
                }
                dataGridView1.Rows.Add(columnId, dr.getUserSelectedTeam(i).teamName);
            }
            
        }

        private void btnSignIn_Click(object sender, EventArgs e)
        {
            enableTeamSelection= dr.login(txtUserName.Text, txtPassword.Text);
            if (enableTeamSelection == false)
            {
                updateDataGridView();
            }
        }
    }
}
